package cn.edu.scau.cmi.zengGuanSheng.hibernate.client;

import java.io.IOException;
import java.util.List;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import cn.edu.scau.cmi.zengGuanSheng.hibernate.domain.Student;
import cn.edu.scau.cmi.zengGuanSheng.hibernate.domain.Teacher;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class HibernateClient extends Application {
	private Stage primaryStage;
    private AnchorPane rootLayout;
    
    @Override
    public void start(Stage primaryStage) {
        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("Hibernate");
        initRootLayout();
    }
    
    public void initRootLayout() {
        try {
            // Load root layout from fxml file.
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(HibernateClient.class.getResource("HibernateScene.fxml"));
            rootLayout = (AnchorPane) loader.load();
            
            // Show the scene containing the root layout.
            Scene scene = new Scene(rootLayout);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

	public static void main(String[] args) {
		// TODO Auto-generated method stub
	/*	SessionFactory sf = new Configuration().configure().buildSessionFactory();
		 
        Session s = sf.openSession();
        s.beginTransaction();
        /*
        //插入数据（学生或老师）
        Teacher teacher = new Teacher();
        teacher.setName("www");
        s.save(teacher);
        
        Student stu = new Student();
        stu.setName("peter");
        stu.setTeacher(teacher);
        s.save(stu);
        */
     /*   Teacher te = (Teacher)s.get(Teacher.class, (long)2);
        //删除
//        s.delete(te);
        //修改
        te.setName("hhh");
        s.update(te);
        Set<Student> students = te.getStudents();
        for(Student st : students) {
        	System.out.println(st.getName());
        }
        
        //查询
        String name = "t";
        Query q =s.createQuery("from Teacher t where t.name = ?");
        q.setString(0, name);
        List<Teacher> ps= q.list();
        for (Teacher p : ps) {
            System.out.println(p.getName());
        }
        
        System.out.println("hello");
        s.getTransaction().commit();
        s.close();
        sf.close();
        */
        launch(args);
	}

}
